/*
* Copyright 2010 Competentum group
*
* Licensed under the GNU General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.competentum.renderers
{
	import com.competentum.controls.ADG;
	import com.competentum.engines.EmployeesEngine;
	import com.competentum.events.EventReasons;
	import com.competentum.events.GroupingEvent;
	import com.competentum.lib.events.CommandEvent;
	import com.competentum.popups.PreferencesPopup;
	import com.competentum.skins.PlusButtonSkin;
	import com.competentum.skins.TextButtonSkin;
	import com.competentum.ui.Messages;
	import com.competentum.vos.AbstractItemVO;
	import com.competentum.vos.EmployeeVO;
	import com.competentum.windows.ScopesManager;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridGroupItemRenderer;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridListData;
	import mx.controls.listClasses.BaseListData;
	import mx.events.AdvancedDataGridEvent;
	
	import spark.components.Button;
	
	/**
	 * Renderer for first grouping column in ADG  
	 * @author OsipovS
	 * 
	 */	
	
	public class GroupingRenderer extends AdvancedDataGridGroupItemRenderer
	{
		private const BUTTON_PADDING_X:int = 10;
		private const BUTTON_PADDING_Y:int = 25;
		
		public var commandType:String;
		
		public function GroupingRenderer()
		{
			super();
		}
		
		protected var addNewItemButton:Button;
		
		override protected function createChildren():void {
			super.createChildren();
			
			if ( !addNewItemButton ) {
				addNewItemButton = new Button();
				addNewItemButton.setStyle("skinClass", TextButtonSkin);
				addNewItemButton.width = 100;
				addNewItemButton.height = 20;
				addNewItemButton.visible = false;
				addNewItemButton.addEventListener(MouseEvent.CLICK, addButtonClick);
				
				addChild(addNewItemButton);				
			}
			
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			if ( disclosureIcon )
			{
				disclosureIcon.width = PreferencesPopup.disclosureIconWidth;
				disclosureIcon.height = PreferencesPopup.disclosureIconHeight;
			}
			
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if ( disclosureIcon )
			{
				addNewItemButton.move(disclosureIcon.x + BUTTON_PADDING_X, disclosureIcon.y + BUTTON_PADDING_Y);
			}
			
		}
		
		private function addButtonClick(event:MouseEvent = null):void
		{
			var grEvent:GroupingEvent = new GroupingEvent(CommandEvent.ACTIVATE_ENGINE, true);
			grEvent.reason = EventReasons.ADD;
			grEvent.commandType = commandType;
			var groupingVO:Object = (listData as AdvancedDataGridListData).item;
			var item:AbstractItemVO = new AbstractItemVO();
			item.name = groupingVO.GroupLabel;
			item.hours = (commandType == EmployeesEngine.TYPE) ? PreferencesPopup.employeeDefaultHours : PreferencesPopup.projectDefaultHours;
			grEvent.valueItem = item;
			grEvent.groupingItem = adgListData.item;
			dispatchEvent(grEvent);
		}
		
		override public function set listData(value:BaseListData):void
		{
			addNewItemButton.visible = false;
			toolTip = "";
			if ( hasEventListener(MouseEvent.MIDDLE_CLICK) )
			{
				removeEventListener(MouseEvent.MIDDLE_CLICK, disclosureMouseDoubleClickHandler);
			}
			
			if ( value && (value is AdvancedDataGridListData) )
			{
				super.listData = value;
				
				var groupingVO:Object = (value as AdvancedDataGridListData).item;
				if (groupingVO && !(groupingVO is AbstractItemVO) && groupingVO.children && (groupingVO.children is ArrayCollection))
				{
					if (groupingVO.children[0] is EmployeeVO)
					{
						addNewItemButton.label = Messages.GROUPING_ADD_EMPLOYEE;
						addNewItemButton.toolTip = Messages.GROUPING_ADD_EMPLOYEE_TOOLTIP;
					}
					else
					{
						addNewItemButton.label = Messages.GROUPING_ADD_PROJECT;
						addNewItemButton.toolTip = Messages.GROUPING_ADD_PROJECT_TOOLTIP;
					}
					addNewItemButton.visible = PreferencesPopup.showGroupAddButtonFlag;
					
					toolTip = Messages.MIDDLE_MOUSE_BUTTON;
					
					if ( !hasEventListener(MouseEvent.MIDDLE_CLICK) ) {
						addEventListener(MouseEvent.MIDDLE_CLICK, disclosureMouseDoubleClickHandler);
					}
				}
			}
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if (listData)
			{
				var groupingVO:Object = (listData as AdvancedDataGridListData).item;
				if (groupingVO.hasOwnProperty('children'))
				{
					label.appendText(' (' + groupingVO.children.length + ')');
				}
			}
		}
		
		public function get adgListData():AdvancedDataGridListData {
			return listData as AdvancedDataGridListData;
		}
		
		private function disclosureMouseDoubleClickHandler(event:MouseEvent):void
		{
			event.stopPropagation();
			
			var listOwner:ADG = ADG(listData.owner);
			
			if (listOwner.isOpening || !listOwner.enabled)
				return;
			
			var open:Boolean = adgListData.open;
			adgListData.open = !open;
			
			listOwner.dispatchAdvancedDataGridEvent(AdvancedDataGridEvent.ITEM_OPENING,
				adgListData.item, //item
				this,   //renderer
				event,  //trigger
				!open,  //opening
				true,   //animate
				true)   //dispatch
		}
	}
}